﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;

namespace Walson.Form
{
    public partial class FlightInstrument
    {
        [ToolboxItem(false)]
        public partial class VerticalSpeedInidicator : UserControl
        {
            private float _VerticalSpeed;
            public double VerticalSpeed
            {
                get
                {
                    return _VerticalSpeed;
                }
                set
                {
                    if (_VerticalSpeed != value)
                    {
                        _VerticalSpeed = (float)value;
                        this.Invalidate();
                    }
                }
            }

            public Bitmap Image
            {
                get;
                private set;
            }

            private Pen _WhitePen;

            public VerticalSpeedInidicator()
            {
                InitializeComponent();
                _WhitePen = new Pen(Brushes.White, 2);
                this.Image = new Bitmap(1, 1);
            }

            protected override void OnPaint(PaintEventArgs e)
            {
                using (Bitmap bmp = new Bitmap(this.Width, this.Height))
                {
                    using (Graphics g = Graphics.FromImage(bmp))
                    {
                        g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;

                        Font font = new Font("Times NewRoman", 9f);

                        g.Clear(Color.FromArgb(16, 16, 49));



                        PointF[] polygon = new PointF[7];
                        polygon[0] = new PointF(0, 0);
                        polygon[1] = new PointF(25, 0);
                        polygon[2] = new PointF(this.Width, this.Height / 2 - 20);
                        polygon[3] = new PointF(this.Width, this.Height / 2 + 20);
                        polygon[4] = new PointF(25, this.Height);
                        polygon[5] = new PointF(0, this.Height);
                        polygon[6] = new PointF(0, 0);
                        g.FillPolygon(new SolidBrush(Color.FromArgb(57, 49, 66)), polygon);

                        g.TranslateTransform(0, this.Height / 2f);

                        PointF[] line = new PointF[2];
                        int gap = this.Height / 10 - 2;
                        for (int i = 0; i < 6; i++)
                        {
                            line[0] = new PointF(15, i * gap);
                            line[1] = new PointF(20, i * gap);
                            g.DrawLine(_WhitePen, line[0], line[1]);

                            line[0] = new PointF(15, -i * gap);
                            line[1] = new PointF(20, -i * gap);
                            g.DrawLine(_WhitePen, line[0], line[1]);

                            g.DrawString(i.ToString(), font, Brushes.White, 2, i * gap - 9);
                            g.DrawString(i.ToString(), font, Brushes.White, 2, -i * gap - 9);
                        }

                        line[0] = new PointF(this.Width, 0);
                        if (VerticalSpeed > 5)
                            line[1] = new PointF(25, -gap * 5);
                        else if (VerticalSpeed < -5)
                            line[1] = new PointF(25, -gap * -5);
                        else
                            line[1] = new PointF(25, -gap * _VerticalSpeed);
                        g.DrawLine(_WhitePen, line[0], line[1]);
                    }
                    this.Image = bmp;
                    e.Graphics.DrawImage(bmp, 0, 0);
                }
                base.OnPaint(e);
            }
        }
    }
}